//app.js
const promisify = require('./utils/promisify')

App({
  onLaunch: function () {
    let that = this
    // that.changeLocal("loginCode", 3)
    that.initData("loginCode", "初始化登录信息")
    that.initData("globalCart", "初始化购物车")
    that.initData("globalFavorite", "初始化收藏夹")
    that.initData("globalOrder", "初始化订单信息")
    that.initData("token", "初始化token")
    that.initData("userId", "初始化userId")
    that.initData("userName", "初始化userName")
  },
  throttle: function (fn, delay) {
    let timer
    return function () {
      let args = arguments
      if (timer) {
        return
      }
      timer = setTimeout(() => {
        fn.apply(this, args)
        timer = null
      }, delay)
    }
  },
  postItemChange: function (item) {
    /**
     * 发送请求
     */
    let that = this
    let url = `https://www.cumthing.world/cumthing/product-cart/addOrDecrease` + that.ToParams({
      userId: that.globalData.userId,
      productId: item.id,
      count: item.num
    })
    promisify(wx.request)({
      url: url,
      method: "POST",
      header: {
        'Authorization': that.globalData.token
      }
    }).then(
      data => {
        console.log(data)
      }
    )
  },
  initData: function (key, message) {
    let that = this
    promisify(wx.getStorage)({
      key: key
    }).then(
      succese => {
        that.globalData[key] = succese.data
      },
      reason => {
        promisify(wx.setStorage)({
          key: key,
          data: that.globalData[key]
        })
        console.log(message)
      }
    )
  },
  loginFirst: function () {
    let that = this
    if (that.globalData.loginCode !== 3) {
      wx.showToast({
        title: "请先登录！",
        icon: "none",
        duration: 2000
      })
      return false
    }
    return true
  },
  changeLocal: function (key, data) {
    promisify(wx.setStorage)({
      key: key,
      data: data
    })
  },
  changeGlobal: function (key, data) {
    this.globalData[key] = data
  },
  changeLocalAndGlobal: function (key, data) {
    this.changeLocal(key, data)
    this.changeGlobal(key, data)
  },
  globalData: {
    userInfo: null,
    loginCode: 0,
    token: "",
    userId: undefined,
    userName: "",
    staticUrl: `https://www.lional.club:6001/`,
    dynamicUrl: `https://www.cumthing.world/`,
    globalCart: [{
        parentId: 1,
        select: false,
        name: "热门",
        items: []
      },
      {
        parentId: 2,
        select: false,
        name: "学习用品",
        items: []
      },
      {
        parentId: 3,
        select: false,
        name: "生活用品",
        items: []
      },
      {
        parentId: 4,
        select: false,
        name: "纪念品",
        items: []
      },
      {
        parentId: 5,
        select: false,
        name: "装饰",
        items: []
      },
      {
        parentId: 6,
        select: false,
        name: "其他",
        items: []
      }
    ],
    globalFavorite: [],
    globalOrders: []
  },
  ToParams: function (obj) {
    /**
     * 转换为params格式字符串
     */
    let that = this
    let string = "?"
    for (let item in obj) {
      string += item + "=" + obj[item] + "&"
    }
    return string
  },
  changeLocalStorage: function (key, data) {
    promisify(wx.setStorage)({
      key: key,
      data: data
    })
  }
})